package com.magist.manage.copyrights.controller;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.magist.core.controller.BaseController;
import com.magist.core.entity.CxtHolder;
import com.magist.core.entity.ResultMessage;
import com.magist.core.exception.BusinessException;
import com.magist.core.util.BeanCopyUtil;
import com.magist.core.util.DateUtil;
import com.magist.core.util.ExcelUtil;
import com.magist.manage.copyrights.dto.CommunicationDto;
import com.magist.manage.copyrights.entity.Communication;
import com.magist.manage.copyrights.export.CommunicationExportDto;
import com.magist.manage.copyrights.service.CommunicationService;
import com.magist.manage.util.AuthFilterUtil;
import com.magist.manage.web.service.UserInfoService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.ObjectUtils;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;
import tk.mybatis.mapper.entity.Example;

import javax.servlet.http.HttpServletResponse;
import java.util.Date;
import java.util.List;

@Api(value="沟通记录",tags={"沟通记录"})
@RestController
@RequestMapping("/rest/communication")
public class RestCommunicationController extends BaseController {

	@Autowired
	private CommunicationService communicationService;
	@Autowired
	private UserInfoService userInfoService;

	@ApiOperation(value = "保存", notes = "保存接口")
	@RequestMapping(value = "save.do", method = RequestMethod.POST)
	public Object save(@ModelAttribute("communicationDto") CommunicationDto communicationDto) {
		Communication communication = new Communication();
		BeanUtils.copyProperties(communicationDto, communication);

		communicationService.doSaveCommunication(communication);
		return ResultMessage.success();
	}

	@ApiOperation(value = "更新", notes = "更新接口")
	@RequestMapping(value = "update.do", method = RequestMethod.POST)
	public Object update(@ModelAttribute("communicationDto") CommunicationDto communicationDto) {
		Communication oldCommunication = communicationService.getById(communicationDto.getId());
		if (oldCommunication == null)
			throw new BusinessException("无效的数据!");
		ResultMessage result = null;
		Communication communication = new Communication();
		BeanUtils.copyProperties(communicationDto, communication);
		communication.setModifyTime(oldCommunication.getModifyTime());
		if (communicationService.doUpdateCommunication(communication))
			result = ResultMessage.success("操作成功");
		else
			result = ResultMessage.error("操作失败!");
		return result;
	}

	@ApiOperation(value = "列表", notes = "分页查询接口")
	@RequestMapping(value = "page.do", method = RequestMethod.GET)
	public Object page(@ModelAttribute("communicationDto") CommunicationDto communicationDto) {
		ResultMessage result = ResultMessage.success();
		Example example = new Example(Communication.class);
		AuthFilterUtil.doAddAuthCondition(example,userInfoService.getById(CxtHolder.getUser().getUserId()));
		example = packExample(example, communicationDto);
		PageInfo<Communication> pageInfo = communicationService.page(example, getPageInfo());
		result.setContent(pageInfo);
		return result;
	}

	@ApiOperation(value = "删除", notes = "删除接口")
	@RequestMapping(value = "del.do", method = RequestMethod.POST)
	public Object del(@ModelAttribute("id") String id) {
		Communication communication = communicationService.getById(id);
		if (communication == null)
			throw new BusinessException("无效的数据!");
		ResultMessage result = null;
		if (communicationService.delete(communication) > 0)
			result = ResultMessage.success("操作成功");
		else
			result = ResultMessage.error("操作失败!");
		return result;
	}

	@ApiOperation(value = "失效", notes = "失效接口")
	@RequestMapping(value = "invalid.do", method = RequestMethod.POST)
	public Object invalid(@ModelAttribute("id") String id) {
		Communication communication = communicationService.getById(id);
		if (communication == null)
			throw new BusinessException("无效的数据!");
		ResultMessage result = null;
		communication.setStatus("F");
		if (communicationService.update(communication) > 0)
			result = ResultMessage.success("操作成功");
		else
			result = ResultMessage.error("操作失败!");
		return result;
	}

	@ApiOperation(value = "批量失效沟通", notes = "批量失效沟通")
	@RequestMapping(value = "batchInvalid.do", method = RequestMethod.POST)
	public Object batchInvalid(@ModelAttribute("invalidIds") String invalidIds) {
		if (StringUtils.isEmpty(invalidIds)) {
			throw new BusinessException("未选择失效数据");
		}
		String[] invalids = invalidIds.split(",");
		ResultMessage result = null;
		for (String id : invalids) {
			Communication communication = communicationService.getById(id);
			if (communication == null)
				throw new BusinessException("无效的数据!");

			communication.setStatus("F");
			if (communicationService.update(communication) > 0)
				result = ResultMessage.success("操作成功");
			else {
				result = ResultMessage.error("操作失败!");
				return result;
			}
		}
		return result;
	}


	@ApiOperation(value = "详情", notes = "详情接口")
	@RequestMapping(value = "info.do", method = RequestMethod.GET)
	public Object info(@ModelAttribute("id") String id) {
		Communication communication = communicationService.getById(id);
		if (communication == null)
			throw new BusinessException("无效的数据!");
		CommunicationDto communicationDto = new CommunicationDto();
		BeanUtils.copyProperties(communication, communicationDto);
		ResultMessage result = ResultMessage.success();
		result.setContent(communicationDto);
		return result;
	}

	private Example packExample(Example example, CommunicationDto communicationDto) {
		Example.Criteria criteria = example.createCriteria();
		criteria.andEqualTo("deleted", "F");


		if(!ObjectUtils.isEmpty(communicationDto.getBegDate())) {
			criteria.andGreaterThanOrEqualTo("commTime", communicationDto.getBegDate());
		}
		Date endDate = communicationDto.getEndDate();

		if(!ObjectUtils.isEmpty(endDate)) {
			// 结束时间为时间末
			endDate = DateUtil.addDays(endDate, 1);
			criteria.andLessThanOrEqualTo("commTime", endDate);
		}
		if(!StringUtils.isEmpty(communicationDto.getObjId())) {
			criteria.andEqualTo("objId", communicationDto.getObjId());
		}
		if(!StringUtils.isEmpty(communicationDto.getCommGoal())) {
			criteria.andEqualTo("commGoal", communicationDto.getCommGoal());
		}
		if(!StringUtils.isEmpty(communicationDto.getCommProgress())) {
			criteria.andEqualTo("commProgress", communicationDto.getCommProgress());
		}
		if(!StringUtils.isEmpty(communicationDto.getCommDifficulty())) {
			criteria.andEqualTo("commDifficulty", communicationDto.getCommDifficulty());
		}
		if(!StringUtils.isEmpty(communicationDto.getNickName())) {
			criteria.andLike("supplyName", "%" + communicationDto.getNickName()+ "%");
		}
		if(!StringUtils.isEmpty(communicationDto.getSupplyId())) {
			criteria.andLike("supplyId", "%" + communicationDto.getSupplyId()+ "%");
		}
		return example;
	}



	@ApiOperation(value = "excel导出", notes = "excel导出")
	@RequestMapping(value = "exportExcel.do", method = RequestMethod.GET)
	public void exportExcel(@ModelAttribute("communicationDto") CommunicationDto communicationDto, HttpServletResponse response) {
        Example example = new Example(Communication.class);
        example = packExample(example, communicationDto);
		AuthFilterUtil.doAddAuthCondition(example,userInfoService.getById(CxtHolder.getUser().getUserId()));
		PageHelper.startPage(1, Integer.MAX_VALUE-1,"SHOW_ID DESC");
		List<Communication> communications = communicationService.find(example);
		List<CommunicationExportDto> commExportDtos = BeanCopyUtil.copy(communications, CommunicationExportDto.class);
		//导出操作
		ExcelUtil.exportExcel(commExportDtos, "沟通列表", "沟通列表", CommunicationExportDto.class, DateUtil.toString(new Date(), DateUtil.yyyy_MM_dd) + "沟通列表.xls", response);
	}

}
